﻿using System;
using models;
using BLL;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace yuejilalala
{
    public partial class Email : System.Web.UI.Page
    {
        UserMarager marager = new UserMarager();
        CartMarager cartMarager = new CartMarager();
        string yxh;
        int irdm;
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = Session["sess"] as User;
            TextBox1.Text = user.Email;
            if (!IsPostBack)
            {
                if (Session["sess"] == null)
                {
                    Response.Redirect("Login.aspx");
                }
                //判定用户是否登陆 登陆显示购物车内容总数
                if (Session["sessid"] != null)
                {

                    J_MiniCartNum.Text = cartMarager.CartCount((Session["sessid"].ToString())).ProPrice.ToString();//显示出购物车内物品.数量
                }
            }
            userDl();
        }

        //登陆在左上方显示用户名方法					
        public void userDl()
        {
            if (Session["sess"] != null)
            {
                //用户存在则显示出来 并且隐藏注册和登陆显示
                User user = Session["sess"] as User;
                hl_dlm.Text = "用户名:" + user.UName;
                hl_dlm.Visible = true;//显示登陆名控件
                hl_dl.Visible = false;//请登陆隐藏
                hl_zc.Visible = false;//注册隐藏
            }

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SmtpClient client = new SmtpClient("smtp.qq.com", 587);
            Random Rdm = new Random();
            //产生0到100000的随机数
            int iRdm = Rdm.Next(100000, 999999);
            MailMessage msg = new MailMessage("1976927876@qq.com", TextBox1.Text, "验证码", iRdm.ToString());
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential basicAuthenticationInfo =
            new System.Net.NetworkCredential("1976927876@qq.com", "czwywozjlbgmbjhb");
            client.Credentials = basicAuthenticationInfo;
            client.EnableSsl = true;
            client.Send(msg);

            Session["YZM"] = iRdm;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            User user = Session["sess"] as User;
            yxh = TextBox1.Text;
            irdm =int.Parse( Session["YZM"].ToString());
            if(TextBox2.Text == irdm.ToString())
            {
                user.Email = yxh;
                marager.updataemails(user).ToString();
                Response.Redirect("emall.aspx");
            }
            else
            {
                Response.Write("<script>alert('验证码错误')</script>");
            }
        }
    }
}